﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Upeu.Matricula.Data.Entities;

namespace Upeu.Matricula.Data.Business
{
    public class NotaBusiness
    {
        public static IList<Nota> GetList(string filter)
        {
            using (UpeuMatriculaDbContext ctx = new UpeuMatriculaDbContext())
            {
                return ctx.Notas.Where(u => u.CursoNot.Contains(filter))
                    .Include(t => t.Student)
                    .Include(t => t.Curso)
                    .ToList();
            }
        }

        public static Nota Create(Nota Nota)
        {
            using (UpeuMatriculaDbContext ctx = new UpeuMatriculaDbContext())
            {
                Nota.Student = ctx.Students.Find(Nota.StudentId);
                Nota.Curso = ctx.Cursos.Find(Nota.CursoId);
                Nota NotaDB = ctx.Notas.Add(Nota);

                ctx.SaveChanges();
                return NotaDB;
            }
        }
        public static Nota Update(Nota Nota)
        {
            using (UpeuMatriculaDbContext ctx = new UpeuMatriculaDbContext())
            {
                Nota.Student = ctx.Students.Find(Nota.StudentId);
                Nota.Curso = ctx.Cursos.Find(Nota.CursoId);
                ctx.Entry(Nota).State = EntityState.Modified;

                ctx.SaveChanges();
                return Nota;
            }
        }
        public static Nota GetById(Guid id)
        {
            using (UpeuMatriculaDbContext ctx = new UpeuMatriculaDbContext())
            {
                Nota NotaDB = ctx.Notas.Find(id);
                if (NotaDB == null)
                {
                    throw new Exception("Nota not found in data base");
                }
                return NotaDB;
            }
        }
        public static void Delete(Guid id)
        {
            using (UpeuMatriculaDbContext ctx = new UpeuMatriculaDbContext())
            {
                Nota NotaDB = ctx.Notas.Find(id);
                if (NotaDB == null)
                {
                    throw new Exception("Nota not found in data base");
                }
                ctx.Notas.Remove(NotaDB);
                ctx.SaveChanges();

            }
        }
    }
}
